package com.example.controller;
import com.example.pojo.*;
import com.example.service.controlPlanService;
import com.example.service.controlTestingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:controlPTesting
 * Description:
 *
 * @Author 赵永峰
 * @Create 2024/5/30 22:52
 * @Version 1.0
 */
@RestController
@Slf4j
public class controlTestingController {
    @Autowired
    private controlTestingService controlTestingService;
    @Autowired
    private controlPlanService controlPlanService;
    //查询“频次检测”页面的树形导航结构
    @PostMapping("controlTestingTreeList")
    public Result controlTestingTreeList(){
        List<ThreeList> threeLists=controlTestingService.controlPlanTreeList();
        return Result.success(threeLists);
    }

    //点击树形结构，把对应该序的数据均筛选出来
    @PostMapping("/controlTestingList")
    public Result controlTestingList(@RequestBody controlTestingPojo controlTestingPojo){
        //筛选列表
        List<controlTestingPojo> controlTestingPojoList=controlTestingService.controlTestingList(controlTestingPojo);
        return Result.success(controlTestingPojoList);
    }
    @PutMapping("/ControlTestingPut")
    public Result ControlTestingPut(@RequestBody controlTestingPojo controlTestingPojo){
        //修改数据
        controlTestingService.putById(controlTestingPojo);
        //把修改的数据再次查出来，反馈给前端
        List<controlTestingPojo> controlTestingPojoList=controlTestingService.controlTestingList(controlTestingPojo);
        return Result.success(controlTestingPojoList);
    }
    @PostMapping("/controlTestingInsertList")
    public Result controlTestingInsertList(@RequestBody List<controlTestingPojo> controlTestingPojoList){
//修改数据，把原来的数据更新，把新数据加入
        controlTestingService.controlTestingInsertC(controlTestingPojoList);
        //把修改的数据再次查出来，反馈给前端
        List<controlTestingPojo> controlTestingPojoListNew=controlTestingService.controlTestingList(controlTestingPojoList.get(0));
        return Result.success(controlTestingPojoListNew);
    }



    //新的数据库用到的函数
    //点击导航，进行查询的函数,根据工序号进行查询
    @PostMapping("/inquiryControlTestingByOp")
    public Result inquiryControlTestingByOp(@RequestBody Map<String,Object> parameters){
        List<controlTestingPojo> controlTestingPojoList=controlTestingService.inquiryControlTestingByOp(parameters);
        return Result.success(controlTestingPojoList);

    }
    //保存用户输入的检测数据
    @PostMapping("/testingValInsert")
    public Result testingValInsert(@RequestBody List<controlTestingPojo> controlTestingPojoList){
        //插入数据
        controlTestingService.testingValInsert(controlTestingPojoList);
        return Result.success();
    }
    //筛选出趋势分析中用户选择的数据
    @PostMapping("/selectNum")
    public Result selectNum(@RequestBody controlTestingPojo controlTestingPojo){
        List<controlTestingPojo> controlTestingPojoList=controlTestingService.selectNum(controlTestingPojo);
        return Result.success(controlTestingPojoList);
    }
    //在更正数据页面中，修改数据，接收的是修改数字信息
    @PostMapping("/testingEditNmb")
    public Result testingEditNmb(@RequestBody Map<String, Object> parameters){
        controlTestingService.testingEditNmb(parameters);
        return Result.success();
    }
    //在更正数据页面中，修改数据，接收的是修改文本信息
    @PostMapping("/testingEditStr")
    public Result testingEditStr(@RequestBody Map<String, Object> parameters){
        controlTestingService.testingEditStr(parameters);
        return Result.success();
    }
    //接受到的是根据Id修改一个字段的内容。用的有：修改作业指导书内容
    @PostMapping("/editOneFiledByControlTestingNew")
    public Result editOneFiledByControlTestingNew(@RequestBody Map<String,Object> parameters){
        controlTestingService.editOneFiledByControlTestingNew(parameters);
        return Result.success(parameters.get("val"));
    }
    //上传图片的函数
    @PostMapping("/addPictureControlTesting")
    public Result addPictureControlTesting(@RequestParam("file") MultipartFile file, @RequestParam Integer id, @RequestParam String fieldName, @RequestParam String fieldVal, @RequestParam String fileName){
        //上传照片的接口
        String allName = controlTestingService.addPicture(file,id,fieldName,fieldVal,fileName);
        return Result.success(fieldVal);
    }
    //下载图片、视频
    @GetMapping("/controlTestingDownload")
    public Result controlTestingDownload(@RequestParam("fileName") String fileName){
        byte[] file= controlTestingService.readFile(fileName);
        return Result.success(file);
    }
    //删除图片、视频
    @PostMapping("/controlTestPictureRemove")
    public Result controlTestPictureRemove(@RequestBody Map<String, Object> parameters) {
        String fileName=controlTestingService.PictureRemove(parameters);
        return Result.success(fileName);
    }
    //根据id，查询控制计划的内容
    @GetMapping("/queryControlPlanControlTesting")
    public Result queryControlPlanControlTesting(@RequestParam("id") Integer id){
        controlPlanPojo  controlPlanPojo=controlPlanService.controlPlanById(id);
        return Result.success(controlPlanPojo);
    }
    //根据传来的字段(机型号），查询共计有多少条
    @PostMapping("/queryNumControlTexting")
    public Result queryNumControlTexting(@RequestBody String filed){
        int num=controlTestingService.queryNumControlTexting(filed);
        return Result.success(num);
    }
    //向检测数据库中的数据进行查询，是输入一个字段名，以及值，符合这一原则的数量是多少
    @PostMapping("/queryNumTextingResult")
    public Result queryNumTextingResult(@RequestBody Map<String,Object> map){
        int num=controlTestingService.queryNumTextingResult(map);
        return Result.success(num);
    }
    //向装配检测日志的数据库中查询，根据一个字段（数据不唯一）
    @PostMapping("/assTestLogInquiryByOneFiled")
    public Result assTestLogInquiryByOneFiled(@RequestBody Map<String,Object> map){
        List<assTestLogPojo> assTestLogPojo=controlTestingService.assTestLogInquiryByOneFiled(map);
        return Result.success(assTestLogPojo);
    }
    //依据两个字段，向后端查询装配检测日志的信息(信息需要唯一）
    @PostMapping("/assTestLogInquiryByTowFiled")
    public Result assTestLogInquiryByTowFiled(@RequestBody Map<String,Object> map){
        assTestLogPojo assTestLogPojo=controlTestingService.assTestLogInquiryByTowFiled(map);
        return Result.success(assTestLogPojo);
    }
    //辅助函数：（检测标准）根据一个字段，查询出符合这个字段所有的数据
    @PostMapping("/controlTestingInquiryOneFiled")
    public Result controlTestingInquiryOneFiled(@RequestBody Map<String,Object> map){
       List<controlTestingPojo>  controlTestingPojos=controlTestingService.controlTestingInquiryOneFiled(map);
       return Result.success(controlTestingPojos);
    }
    //辅助函数：（检测结果）根据三个字段，询符合等于这三个字段的数据
    @PostMapping("/controlTestingDataInquiryThreeFiled")
    public Result controlTestingDataInquiryThreeFiled(@RequestBody Map<String,Object> map){
        List<controlTestingPojo> controlTestingPojoList=controlTestingService.controlTestingDataInquiryThreeFiled(map);
        return Result.success(controlTestingPojoList);
    }
    //辅助函数，(检测日志）向装配检测日志的数据库中一次插入多个数据
    @PostMapping("/assTestLogInsertAll")
    public Result assTestLogInsertAll(@RequestBody List<assTestLogPojo> assTestLogPojoList){
        controlTestingService.assTestLogInsertAll(assTestLogPojoList);
        return Result.success("插入成功");
    }
    //辅助函数：（检测日志）根据一个字段，按照另一个字段从大到小排序，查询出第一条（主要用在了查询最后计算日期的场景上）
    @PostMapping("/assTestLogEndDateCount")
    public Result assTestLogEndDateCount(@RequestBody Map<String,Object> map){
        assTestLogPojo assTestLogPojo=controlTestingService.assTestLogEndDateCount(map);
        return Result.success(assTestLogPojo);
    }
    //辅助函数：（检测数据）根据一个字段，查询这个字段下A值到B值的数据，主要用在查询两个日期之间的值，或者大于A，小于等于B的值；
    @PostMapping("/testingQueryOneFieldByAtoB")
    public Result testingQueryOneFieldByAtoB(@RequestBody Map<String,Object> map){
        List<controlTestingPojo> controlTestingPojoList=controlTestingService.queryOneFieldByAtoB(map);
        return Result.success(controlTestingPojoList);
    }
    //辅助函数：（检测数据）根据一个字段A等于某一值，B字段从大到小排序，筛选n行；
    @PostMapping("/testingQueryFieldAndBNum")
    public Result testingQueryFieldAndBNum(@RequestBody Map<String,Object> map){
        List<controlTestingPojo>controlTestingPojoList=controlTestingService.testingQueryFieldAndBNum(map);
        return Result.success(controlTestingPojoList);
    }
    //(检测日志)根据用户点击的刷新按钮，更新最新的计算日期：type是类型，例如：EB05PPK，或者其他的类型数据，doe:是检测类型中的第二位，例如，EB05PPK，M，就是代表要查询月度EB05PPK的值，也就是模糊查询EB05PPK-M，排序是dateUse列创建时间倒序排序；
    @PostMapping("/assTestLogRefreshPPKDate")
    public Result assTestLogRefreshPPKDate(@RequestBody Map<String,Object> map){
        assTestLogPojo assTestLogPojoList=controlTestingService.assTestLogRefreshPPKDate(map);
        return Result.success(assTestLogPojoList);
    }
    //辅助函数：（检测数据）根据一个字段，查询等于这个字段的所有数据
    @PostMapping("/testingQueryOneFiled")
    public Result testingQueryOneFiled(@RequestBody Map<String,Object> map){
        List<controlTestingPojo> controlTestingPojoList=controlTestingService.testingQueryOneFiled(map);
        return Result.success();
    }
    //定时函数：定时计算EB05的每日需要的检测数量、实际完成数量等；
//    @Scheduled(fixedRate = 10000) // 10000毫秒 = 10秒
    public void assTesLogToday(){
        //建立数据结构
        // 创建一个Map来模拟对象字面量
        Map<String, Object> everyDayTestEB05 = new HashMap<>();

        // 向Map中添加键值对
        everyDayTestEB05.put("num", ""); // 空字符串表示未设置的值
        everyDayTestEB05.put("actual", "");
        everyDayTestEB05.put("ok", "");
        everyDayTestEB05.put("ng", "");
        everyDayTestEB05.put("reserve1", "");
        everyDayTestEB05.put("reserve2", "");
        everyDayTestEB05.put("reserve3", "");
        //读取装配车间检测中的当日需要检测的数量是多少
        Map<String,Object> map=new HashMap<>();
        map.put("oneFiledName","drawing");
        map.put("oneFiledVal","1000100XEB05");
        List<controlTestingPojo>  controlTestingPojos=controlTestingService.controlTestingInquiryOneFiled(map);
        //

    }
}
